/* EasyWay Game Engine
 * Copyright (C) 2007 Daniele Paggi.
 *  
 * Written by: 2007 Daniele Paggi<dshnt@hotmail.com>
 *   
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation; either version 2 of
 * the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Library General Public License for more details.
 * 
 * You should have received a copy of the GNU Library General Public
 * License along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */
package org.easyway.editor.forms.components;

import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;

import org.easyway.editor.forms.UEditor;
import org.easyway.editor.forms.components.interfaces.IEnable;

public abstract class Button extends JPanel implements IEnable {

	private static final long serialVersionUID = 1L;

	protected JLabel label = null;

	protected JButton button = null;

	public Button(String labelText, String buttonText) {
		super();
		initialize();
		if (labelText != null) {
			label.setText(labelText);
		} else {
			remove(label);
			label = null;
		}
		button.setText(buttonText);
	}

	/**
	 * This is the default constructor
	 */
	public Button() {
		super();
		initialize();
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		label = new JLabel();
		label.setText("text");
		label.setHorizontalAlignment(SwingConstants.LEFT);
		GridLayout gridLayout = new GridLayout();
		gridLayout.setRows(1);
		gridLayout.setColumns(2);
		this.setLayout(gridLayout);
		this.setSize(300, 200);
		this.add(label, null);
		this.add(getButton(), null);
		UEditor.getLeftPanel().add(this);
	}

	/**
	 * This method initializes button
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getButton() {
		if (button == null) {
			button = new JButton();
			button.setText("ok");
			final Color oldColor = button.getBackground();
			final Color greenColor = new Color(255, 128, 128);
			final Color blackColor = new Color(0, 0, 0);
			MouseListener ml = new MouseListener() {

				public void mouseClicked(MouseEvent e) {
				}

				public void mouseEntered(MouseEvent e) {
					button.setForeground(greenColor);
					setBackground(greenColor);
				}

				public void mouseExited(MouseEvent e) {
					button.setForeground(blackColor);
					setBackground(oldColor);
				}

				public void mousePressed(MouseEvent e) {
				}

				public void mouseReleased(MouseEvent e) {
				}

			};
			addMouseListener(ml);
			button.addMouseListener(ml);
			button.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					new Thread() {
						public void run() {
							command();
						}
					}.start();
				}
			});
		}
		return button;
	}

	public void setEnabled(boolean value) {
		button.setEnabled(value);
	}

	protected static void removeAllFromPanel() {
		UEditor.getLeftPanel().removeAll();
		UEditor.getLeftPanel().repaint();
	}

	public abstract void command();
}
